<template>
  <div class="saech_filer_box">
    <el-form
      ref="form"
      :model="searchForm"
      label-position="top"
      label-width="auto"
      :inline="false"
      size="small"
    >
      <el-row :gutter="20">
        <el-col :span="24" :offset="0" style="margin-bottom: 10px">筛选</el-col>

        <el-col :span="24" :offset="0">
          <el-form-item label="批次号" prop="batchNumber">
            <el-select
              v-model="searchForm.batchNumber"
              class="w100"
              clearable
              collapse-tags
              filterable
              placeholder="请选择"
              size="mini"
              value-key="token"
            >
              <el-option
                v-for="item in tableDataUniqBy('batchNumber')"
                :key="item.batchNumber"
                :label="item.batchNumber"
                :value="item.batchNumber"
              />
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="24" :offset="0">
          <el-form-item label="零件号" prop="partNumber">
            <el-select
              v-model="searchForm.partNumber"
              class="w100"
              clearable
              collapse-tags
              filterable
              placeholder="请选择"
              size="mini"
              value-key="token"
            >
              <el-option
                v-for="item in tableDataUniqBy('partNumber')"
                :key="item.partNumber"
                :label="item.partNumber"
                :value="item.partNumber"
              />
            </el-select> </el-form-item
        ></el-col>

        <el-col :span="24" :offset="0">
          <el-form-item label="工序号" prop="processNumber">
            <el-select
              v-model="searchForm.processNumber"
              class="w100"
              clearable
              collapse-tags
              filterable
              placeholder="请选择"
              size="mini"
              value-key="token"
            >
              <el-option
                v-for="item in tableDataUniqBy('processNumber')"
                :key="item.processNumber"
                :label="item.processNumber"
                :value="item.processNumber"
              />
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="24" :offset="0">
          <el-form-item label="状态" prop="stateList">
            <el-select
              v-model="searchForm.stateList"
              multiple
              class="w100"
              placeholder="请选择"
              filterable
            >
              <el-option key="生产" label="生产" value="生产" />
              <el-option key="物料" label="物料" value="物料" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" :offset="0">
          <el-form-item label="日期：">
            <div class="flex-x">
              <el-date-picker
                v-model="searchForm.startDate"
                class="w100"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="mini"
                placeholder="开始日期"
              />
              <i class="el-icon-right" style="color: rgba(0, 0, 0, 0.25)" />
              <el-date-picker
                v-model="searchForm.endDate"
                class="w100"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                size="mini"
                placeholder="结束日期"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="search_btns flex-x flex-space">
      <el-button style="width: 49%" class="reset" size="mini" @click="reSet"
        >重置</el-button
      >
      <el-button
        style="width: 49%"
        class="submit"
        size="mini"
        type="primary"
        @click="submitForm"
        >提交</el-button
      >
    </div>
  </div>
</template>

<script>
import { getTemplate } from "@/api/technology";
import { currentDate3 } from "@/utils/currentDate";
import _ from "lodash";
export default {
  props: {
    allData: {
      type: Array,
      default: () => []
    },
    userList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      searchForm: {
        batchNumber: "",
        model: "",
        partNumber: "",
        processNumber: "",
        startDate: "",
        endDate: "",
        // startDate: currentDate(),
        // endDate: currentDate({
        //   hours: 23,
        //   minute: 59,
        //   second: 59,
        // }),
        stateList: ["物料"]
      }
    };
  },
  computed: {
    tableDataUniqBy() {
      return function(key = "processNumber") {
        return _.uniqBy(this.allData, key);
      };
    }
  },
  mounted() {
    this.getTemplate();
  },
  methods: {
    getTemplate() {
      getTemplate({
        page: 1,
        pageSize: 0
      })
        .then(r => {
          const { data } = r;

          this.tableData = _.uniqBy(data.results, "author");
        })
        .finally(f => {});
    },

    // 重置
    reSet() {
      this.searchForm = {
        batchNumber: "",
        model: "",
        partNumber: "",
        processNumber: "",
        startDate: "",
        endDate: "",
        // startDate: currentDate(),
        // endDate: currentDate({
        //   hours: 23,
        //   minute: 59,
        //   second: 59,
        // }),
        stateList: ["物料"]
      };
      this.$emit("setFilterList");
    },
    submitForm() {
      if (
        this.searchForm.processNumber ||
        this.searchForm.partNumber ||
        this.searchForm.model ||
        this.searchForm.batchNumber ||
        this.searchForm.startDate ||
        this.searchForm.endDate ||
        this.searchForm.stateList
      ) {
        this.$emit("setFilterList", {
          ...this.searchForm,
          startDate: currentDate3(this.searchForm.startDate),
          endDate: currentDate3(this.searchForm.endDate)
        });
      } else {
        this.$emit("setFilterList");
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.saech_filer_box {
  position: relative;
  height: 100%;
  /deep/.el-form-item {
    margin-bottom: 10px;
    .el-form-item__label {
      padding-bottom: 0 !important;
    }
  }
  .search_btns {
    width: 100%;
    position: absolute;
    bottom: 0;
    padding: 10px;
    left: 0;
  }
}
</style>
